// $_FILEHEADER_BEGIN ***************************
// 版权声明:Timer
// Copyright © 2015 - Timer All Rights Reserved
// 文件名称: ARDetailView.m
// 创建日期: 15/12/13
// 创 建 人: 甄鑫
// 文件说明: <#content#>
// $_FILEHEADER_END ******************************


#import "ARDetailView.h"
#import "ARViewHepler.h"
#define HeaderButtonTag         (20160117)

@implementation ARDetailView
{
    IBOutlet UITableView *_mpMainTableView;
    IBOutlet UIView *mpHeaderView;
    ARDetailCell * cell;
}

- (void)awakeFromNib
{
    mpHeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 64);
    [_mpMainTableView setTableHeaderView:mpHeaderView];
    
}

#pragma mark - **************** UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _mpMainTableView)
    {
        if (_mpMainTableView.contentOffset.y < - 80)
        {
            if ([self.delegate respondsToSelector:@selector(ARDetailViewShowUpView)])
            {
                [self.delegate ARDetailViewShowUpView];
            }
        }
    }
}
#pragma mark - **************** UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT-45-45-64;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell = [[ARDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
    [cell cellSetType:self.pageType andBase:self.baseType and:tableView];
    cell.delegate = self;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 45;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSArray *array;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 45)];
    headerView.tag = HeaderButtonTag + 100;
    headerView.backgroundColor = [UIColor whiteColor];
    if (self.baseType == 1) {
        if (self.pageType == 7||self.pageType == 6||self.pageType == 2||self.pageType == 700||self.pageType == 1||self.pageType == 0||self.pageType == 11||self.pageType == 12) {
            array = @[@"出借记录",@"还款计划",@"借款信息"];
        }else{
        array = @[@"回款计划",@"出借记录",@"还款计划",@"借款信息"];
        }
    }else{
        array = @[@"出借记录",@"还款计划",@"借款信息"];
    }
    CGFloat width = (CGFloat)SCREEN_WIDTH/array.count;
    for (int i=0; i<array.count; i++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i*width, 0, width, 44)];
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        button.tag = HeaderButtonTag+i;
        [button addTarget:self action:@selector(headerButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:button];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(button.frame.origin.x, headerView.frame.size.height-1, width, 1)];
        line.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
        line.tag = button.tag+10;
        [headerView addSubview:line];
        if (i==0) {
            [button setTitleColor:[ARViewHepler getNavColor:self.pageType] forState:UIControlStateNormal];
            line.backgroundColor = [ARViewHepler getNavColor:self.pageType];
        }
    }
    return headerView;
}
#pragma mark - action
- (void)headerButtonClick:(UIButton *)sender{
    UIView *headerView = (UIView *)[self viewWithTag:HeaderButtonTag+100];
    NSInteger i = sender.tag - HeaderButtonTag;
    [cell cellScrollWithIndex:i];
    for (UIView *view in headerView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)view;
            UIView *lineView = (UIView *)[self viewWithTag:button.tag+10];
            if (sender.tag == button.tag) {
                [sender setTitleColor:UIColorFromRGB(0xfe6b6b) forState:UIControlStateNormal];
                lineView.backgroundColor = [ARViewHepler getNavColor:self.pageType];
            }else{
                [button setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
                lineView.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
            }
        }
    }
}
#pragma mark - ARCellDelegate
- (void)cellScrollIndex:(NSInteger)index{
    UIView *headerView = (UIView *)[self viewWithTag:HeaderButtonTag+100];
    UIButton *sender = (UIButton *)[self viewWithTag:HeaderButtonTag+index];
    for (UIView *view in headerView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)view;
            UIView *lineView = (UIView *)[self viewWithTag:button.tag+10];
            if (sender.tag == button.tag) {
                [sender setTitleColor:[ARViewHepler getNavColor:self.pageType] forState:UIControlStateNormal];
                lineView.backgroundColor = [ARViewHepler getNavColor:self.pageType];
            }else{
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                lineView.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
            }
        }
    }

}
- (void)setCellData:(id)data withType:(ENUM_Detail_Type)typ{
    switch (typ) {
        case Detail_ChuJie:
        {
            [cell setSubViewChuJieData:data];
        }
            break;
        case Detail_HuanKuan:
        {
            [cell setSubViewHuanKuanData:data];
        }
            break;
        case Detail_HuiKuan:{
            [cell setSubViewHuiKuanData:data];
        }
            break;
        case Detail_JieKuan:{
            [cell setSubViewJieKuanData:data];
        }
            break;
        default:
            break;
    }
    

}
@end
